/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flightlifter;

import java.awt.geom.Line2D;
import java.util.Vector;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Matt
 */
public class Explosive extends Node {

    Point a;
    boolean detonated = false;
    Vector<Node> constraints;
    Vector<Node> nodes;
    Vector<Node> toRemove;



    public Explosive(Point a, Vector<Node> nodes, Vector<Node> constraints, Vector<Node> toRemove) {
        this.a = a;
        this.nodes = nodes;
        this.constraints = constraints;
        this.toRemove = toRemove;
        red = 1.f;
        green = .75f;
    }

    public void physics() {

        if (a.pos.y < .1f && detonated == false) {
            detonate();
        }

    }

    private void detonate() {
        detonated = true;

        LineConstraint l;
        Point p;
        float dist;
        PVector toward;

        /*
        for (Node n : constraints) {
        if (n instanceof LineConstraint) {
        l = (LineConstraint) n;
        //get distance to the point
        dist = PVector.sub(a.pos, l.getCentroid()).mag();
        if (dist < 20) {
         if (Math.random() < dist/20.f) toRemove.add(l);
        }

        }
        }
         */

        for (Node n : nodes) {
            if (n instanceof Point) {
                p = (Point) n;
                //get distance to the point
                toward = PVector.sub( p.getCentroid(),a.pos);

                dist = toward.mag();

                toward.div(dist*dist);
                toward.mult(250000*a.mass);
                p.impulse(toward);
                System.out.println("impulse");
            }
        }


    }

    @Override
    public void render(int mode) {
        GL11.glPushMatrix();

        GL11.glTranslatef(a.pos.x, a.pos.y, a.pos.z);

        GL11.glScalef(.4f, .4f, .4f);
        GL11.glColor3f(red, green, blue);

        Draw.box();

        GL11.glPopMatrix();
    }
    public Explosive(String[] line, Vector<Node> nodes,Vector<Node> constraints,Vector<Node> toRemove) {
        this((Point) nodes.elementAt(Integer.parseInt(line[1])),nodes,constraints,toRemove);
    }
    @Override
    public String pickle(Vector<Node> nodes) {
        return "X,"
                + nodes.indexOf(a) + ",";
    }

    @Override
    public PVector getCentroid() {
        return a.pos;
    }

    @Override
    public PVector getScreenSpaceCentroid() {
        float[] ret = GLProject.project(a.pos.x, a.pos.y, a.pos.z);
        return new PVector(ret[0], ret[1], 0);
    }

    public boolean dependsOn(Point p){
        if (p == a) return true;
        else return false;
    }
}
